using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Planar.PT120.Tray
{
    public partial class Calibrate : Form
    {
        #region Member properties and fields
        private double _CalibrationX = 0.0;
        public double CalibrationX
        {
            get 
            {
                if (_Step == CalibrationStep.Complete)
                {
                    return _CalibrationX / 4;
                }
                else return 0.0;
            }
        }

        private double _CalibrationY = 0.0;
        public double CalibrationY
        {
            get 
            {
                if (_Step == CalibrationStep.Complete)
                {
                    return _CalibrationY / 4;
                }
                else return 0.0;
            }
        } 
        #endregion

        #region Load
        public Calibrate()
        {
            InitializeComponent();
        }
        private void Calibrate_Load(object sender, EventArgs e)
        {
            Screen s = Screen.FromControl(this);
            MouseSimulator.CalibrationX = 0.0;
            MouseSimulator.CalibrationY = 0.0;

            this.WindowState = FormWindowState.Maximized;
            this.Left = s.Bounds.Left;
            this.Top = s.Bounds.Top;
            this.Width = s.Bounds.Width;
            this.Height = s.Bounds.Height;

            PrepareStep();

        } 
        #endregion

        #region Prepare Step
        private CalibrationStep _Step = CalibrationStep.Step1;
        private void PrepareStep()
        {
            pictureBox1.Visible = false;
            pictureBox2.Visible = false;
            pictureBox3.Visible = false;
            pictureBox4.Visible = false;
            switch (_Step)
            {
                case (CalibrationStep.Step1):
                    {
                        pictureBox1.Visible = true;
                        break;
                    }
                case (CalibrationStep.Step2):
                    {
                        pictureBox2.Visible = true;
                        break;
                    }
                case (CalibrationStep.Step3):
                    {
                        pictureBox3.Visible = true;
                        break;
                    }
                case (CalibrationStep.Step4):
                    {
                        pictureBox4.Visible = true;
                        break;
                    }
                case (CalibrationStep.Complete):
                    {
                        DialogResult = DialogResult.OK;
                        Close();
                        break;
                    }
            }
        } 
        #endregion

        #region Handle keyboard input
        private void Calibrate_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 27)
            {
                Close();
            }
        } 
        #endregion

        #region Handle calibration clicks
        private const double Padding = 12;
        private const double Dimensions = 64;
        private void Calibrate_MouseDown(object sender, MouseEventArgs e)
        {
            //this.WindowState = FormWindowState.Minimized;
            Screen s = Screen.FromControl(this);
            double CenterX = 0.0;
            double CenterY = 0.0;
            switch (_Step)
            {
                case (CalibrationStep.Step1):
                    {
                        CenterX = s.Bounds.Left + Padding + (Dimensions / 2);
                        CenterY = s.Bounds.Top + Padding + (Dimensions / 2);

                        _Step = CalibrationStep.Step2;
                        break;
                    }
                case (CalibrationStep.Step2):
                    {
                        CenterX = s.Bounds.Right - (Padding + (Dimensions / 2));
                        CenterY = s.Bounds.Top + (Padding + (Dimensions / 2));

                        _Step = CalibrationStep.Step3;
                        break;
                    }
                case (CalibrationStep.Step3):
                    {
                        CenterX = s.Bounds.Left + Padding + (Dimensions / 2);
                        CenterY = s.Bounds.Bottom - (Padding + (Dimensions / 2));

                        _Step = CalibrationStep.Step4;
                        break;
                    }
                case (CalibrationStep.Step4):
                    {
                        CenterX = s.Bounds.Right - (Padding + (Dimensions / 2));
                        CenterY = s.Bounds.Bottom - (Padding + (Dimensions / 2));

                        _Step = CalibrationStep.Complete;
                        break;
                    }
            }

            _CalibrationX += e.X - CenterX;
            _CalibrationY += e.Y - CenterY;

            PrepareStep();
        }
        private void CalibrateImage_MouseDown(object sender, MouseEventArgs e)
        {
            Screen s = Screen.FromControl(this);
            double CenterX = Dimensions / 2;
            double CenterY = Dimensions / 2;
            switch (_Step)
            {
                case (CalibrationStep.Step1):
                    {
                        _Step = CalibrationStep.Step2;
                        break;
                    }
                case (CalibrationStep.Step2):
                    {
                        _Step = CalibrationStep.Step3;
                        break;
                    }
                case (CalibrationStep.Step3):
                    {
                        _Step = CalibrationStep.Step4;
                        break;
                    }
                case (CalibrationStep.Step4):
                    {
                        _Step = CalibrationStep.Complete;
                        break;
                    }
            }

            _CalibrationX += e.X - CenterX;
            _CalibrationY += e.Y - CenterY;

            PrepareStep();
        }
        #endregion
    }
}